package Servlet;

import Bean.LoginBean;
import Dao.Impl.ManDaoImpl;
import Dao.Impl.StuDaoImpl;
import Dao.Impl.TeaDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        HttpSession session = req.getSession();
        String sessionCode = (String)session.getAttribute("safecode");

        LoginBean user = new LoginBean();
        user.setId(req.getParameter("zhanghao"));
        user.setPassword(req.getParameter("mima"));
        String code=req.getParameter("code");

        StuDaoImpl stu_login=new StuDaoImpl();
        TeaDaoImpl tea_login=new TeaDaoImpl();
        ManDaoImpl man_login=new ManDaoImpl();

        req.setAttribute("Msg1", "layui-this");
        req.setAttribute("Msg11", "layui-show");

        try {
            if(stu_login.Login(user)!=null){
                if(code.equals(sessionCode)) {
                    req.setAttribute("uzhanghao", user.getId());
                    req.setAttribute("umima", user.getPassword());
                    req.getRequestDispatcher("login_welcome.jsp").forward(req, resp);
                }
                else{
                    req.setAttribute("Msg", "123");
                    req.getRequestDispatcher("login.jsp").forward(req,resp);
                }
            }
            else if(tea_login.Login(user)!=null){
                if(code.equals(sessionCode)) {
                    req.setAttribute("uzhanghao", user.getId());
                    req.setAttribute("umima", user.getPassword());
                    req.getRequestDispatcher("tea_welcome.jsp").forward(req, resp);
                }
                else{
                    req.setAttribute("Msg", "123");
                    req.getRequestDispatcher("login.jsp").forward(req,resp);
                }
            }
            else if(man_login.Login(user)!=null){
                if(code.equals(sessionCode)) {
                    req.setAttribute("uzhanghao", user.getId());
                    req.setAttribute("umima", user.getPassword());
                    req.getRequestDispatcher("man_welcome.jsp").forward(req, resp);
                }
                else{
                    req.setAttribute("Msg", "123");
                    req.getRequestDispatcher("login.jsp").forward(req,resp);
                }
            }
            else {
                req.setAttribute("Msg", "123");
                req.getRequestDispatcher("login.jsp").forward(req,resp);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }
}
